﻿using System.Collections.Generic;

namespace Red.Launcher.ViewModels.Workspaces.Database
{
    public class UserToClientSpecs : Record
    {
        #region Inherited Properties

        public override Metadata Metadata { get { return METADATA; } }
        public override string Icon { get { return "appbar_user"; } }
        public override IEnumerable<object> Values { get { return new List<object>() { UserId }; } }

        #endregion Inherited Properties

        #region Public Properties

        public static Metadata METADATA { get; private set; }

        public int UserId { get; set; }

        #endregion Public Properties

        #region C-Tor

        static UserToClientSpecs()
        {
            METADATA = new Metadata()
            {
                Table = "UsersToClientSpecs",
                Keys = new List<string>() { User.METADATA.ForeignKey },
                CreateDefault = () => { return null; },
                CreateFromDatabaseRow = ( Dictionary<string, object> data ) =>
                {
                    return new UserToClientSpecs()
                    {
                        Id = (int)data[ "utcs_id" ],
                        UserId = (int)data[ User.METADATA.ForeignKey ],
                        Name = (string)data[ "display_name" ]
                    };
                },
                GenerateSelectSql = GenerateSelectSql
            };
        }

        public UserToClientSpecs()
        {
        }

        public UserToClientSpecs( User user )
        {
            UserId = user.Id;
            Name = user.Name;
        }

        #endregion C-Tor

        #region Private Methods

        private static Sql.Select GenerateSelectSql( Record parent )
        {
            Sql.Select.Table source = Sql.Select.CreateTable( User.METADATA.Table, "a" );

            Sql.Select query = new Sql.Select();
            query.Sources.Add( source );

            Sql.IAlias joinSource = query.AddStandardChildJoin( User.METADATA, UserToClientSpecs.METADATA, source, P4ClientSpecification.METADATA, parent.Id );

            query.AddColumn( "id", source, User.METADATA.ForeignKey );
            query.AddColumn( "id", joinSource, "utcs_id" );
            query.AddColumn( "username_win", source, "display_name" );

            return query;
        }

        #endregion Private Methods

        #region Comparison

        public override bool Equals( object obj )
        {
            if ( obj == null )
                return false;

            Record record = obj as Record;
            if ( record == null )
                return false;

            if ( ( UserId == record.Id ) && ( Name == record.Name ) )
                return true;

            UserToClientSpecs other = obj as UserToClientSpecs;
            return Equals( other );
        }

        public bool Equals( UserToClientSpecs other )
        {
            if ( other == null )
                return false;

            return ( UserId == other.UserId ) && ( Name == other.Name );
        }

        public override int GetHashCode()
        {
            return UserId ^ Name.GetHashCode();
        }

        #endregion Comparison
    }
}
